package com.koala.learn.threadlearn;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yantingrui
 * @date 2022/2/22   20:50
 */
@Slf4j
public class Cook implements Runnable {

    private final String name;

    // 限制厨师人数
    private final AtomicInteger count = new AtomicInteger(3);

    public Cook(String name) {
        this.name = name;
    }

    /**
     * 每次只能一个任务进去
     */
    @SneakyThrows
    public void cook(String work) {
        while (count.getAndDecrement() > 0) {
            int current = count.get();
            log.info("剩余工作空间{}, {}工作了 1000ms", current + 1, work);
            // 工作时间
            Thread.sleep(1000);
            // 工作完成后任务赋值回去
            count.incrementAndGet();
//            log.info("线程{}完成工作 剩余工作空间{}",work,finished);
            return;
        }
//        count.incrementAndGet();
//        log.info("线程太忙了");
        return;
    }

    @Override
    public void run() {
        log.info("{}开始工作 ", name);
        cook(name);
    }
}
